﻿using SAIM.Models.BLL;
using SAIM.Models.DAL;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace SAIM.Models
{
    public class AddViewModel
    {
        [Required]
        [Display(Name = "UPN")]
        public string Id { get; set; }

        [Required]
        [Display(Name = "Nombre completo")]
        public string UserName { get; set; }

        [Required]
        [EmailAddress(ErrorMessage = "Ingrese una dirección de correo electrónico válida")]
        [Display(Name = "Correo electrónico")]
        public string Email { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "El número de caracteres de {0} debe ser al menos {2}.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Contraseña")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirmar contraseña")]
        [Compare("Password", ErrorMessage = "La contraseña y la contraseña de confirmación no coinciden.")]
        public string ConfirmPassword { get; set; }
    }

    public class SearchViewModel
    {
        [Display(Name = "UPN")]
        public string Id { get; set; }

        [Display(Name = "Nombre")]
        public string UserName { get; set; }

    }

    public class _GetViewModel
    {
        public List<AspNetUser> users { get; set; } 

        public _GetViewModel(List<AspNetUsersDAL> _users)
        {
            users = new List<AspNetUser>();
            foreach (AspNetUsersDAL user in _users)
            {
                users.Add(new AspNetUser()
                {
                    Id = user.Id,
                    UserName = user.UserName,
                    Email = user.Email,
                    url = "/Users/Manage/?id=" + user.Id
                });
            }
        }
    }

    public class UserManageViewModel
    {
        public string Id { get; set; }

        public string UserName { get; set; }

        public string Email { get; set; }

        public string Profile { get; set; }

        public UserManageViewModel() { }

        public UserManageViewModel(AspNetUsersDAL user)
        {
            Id = user.Id;
            UserName = user.UserName;
            Email = user.Email;
            Profile = user.AspNetProfiles != null ? user.AspNetProfiles.Name : string.Empty;
        }
    }

    public class UserChangeProfileViewModel
    {
        private List<AspNetProfilesDAL> _Profiles;

        [Required]
        [Display(Name = "Perfil")]
        public long Profile { get; set; }
        [Required]
        [Display(Name = "UPN")]
        public string Id { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        public string ProfileName { get; set; }

        public IEnumerable<System.Web.Mvc.SelectListItem> Profiles
        {
            get 
            {
                _Profiles.Sort((p1, p2) => p1.Name == ProfileName ? -1 : p2.Name == ProfileName ? 1 : p1.Name.CompareTo(p2.Name));
                return new System.Web.Mvc.SelectList(_Profiles, "Id", "Name"); 
            }
        }

        public UserChangeProfileViewModel()
        {
            using (ProfilesBLL profiles = new ProfilesBLL())
            {
                _Profiles = profiles.Value ?? new List<AspNetProfilesDAL>();
            }
        }
    }

    public class RolesUsersViewModel
    {
        [Required]
        [Display(Name = "UPN del usuario")]
        public string Id { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        public string Profile { get; set; }
        [Required]
        [Display(Name = "Rol de usuario")]
        public string NewRole { get; set; }
        [Required]
        [Display(Name= "Eliminar rol de usuario")]
        public bool RemoveRole { get; set; }

        public RolesUsersViewModel() { }

        public RolesUsersViewModel(AspNetUsersDAL user)
        {
            Id = user.Id;
            UserName = user.UserName;
            Email = user.Email;
            Profile = user.AspNetProfiles != null ? user.AspNetProfiles.Name : string.Empty;
        }
    }

    #region Clases auxiliares
    public class AspNetUser
    {
        public string Id { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        public string url { get; set; }
    }
    #endregion
}